package com.horoscope.astrology.zodiac.signs.free.view;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.horoscope.astrology.zodiac.signs.free.AppSettingManager;
import com.horoscope.astrology.zodiac.signs.free.R;
import com.jeremyliao.liveeventbus.LiveEventBus;

import org.jetbrains.annotations.NotNull;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

public class DatePicker {

    private Context mContext;

    public DatePicker(Context context) {
        mContext = context;
    }

    /**
     * 只有Profile页面点击时显示MMKV所存储的值，其它页面一律显示18年前的今天
     * type = 0 存储生日
     * type = 1 18年前今天
     */
    public void ShowDatePick(int type, final String className) {
        // 自定义AlertDialog
        final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        View view = LayoutInflater.from(mContext).inflate(R.layout.date_pick, null);
        builder.setView(view);
        final AlertDialog alertDialog = builder.create();
        alertDialog.show();

        final CustomDatePicker datePicker = initDatePicker(view, type);
        TextView textView = view.findViewById(R.id.done);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt("year", datePicker.getYear());
                bundle.putInt("moth", datePicker.getMonth() + 1);
                bundle.putInt("day", datePicker.getDayOfMonth());
                LiveEventBus.get("AlertDialogDate" + className, Bundle.class).post(bundle);
                alertDialog.dismiss();
            }
        });

        Window window = alertDialog.getWindow();
        if (Objects.requireNonNull(window).getAttributes() != null) {
            WindowManager.LayoutParams params = window.getAttributes();
            window.setGravity(Gravity.BOTTOM);
            window.setBackgroundDrawableResource(R.drawable.corners_bg);
            Point point = new Point();
            window.getWindowManager().getDefaultDisplay().getSize(point);
            params.width = point.x;
            window.setWindowAnimations(R.style.GenderSelectorAnimation);
            window.setAttributes(params);
        }
    }

    @NotNull
    private CustomDatePicker initDatePicker(View view, int type) {
        final CustomDatePicker datePicker = view.findViewById(R.id.date_picker);
        datePicker.setPickerMargin(0);
        datePicker.setDividerColor(ContextCompat.getColor(mContext, R.color.transparent));
        datePicker.setMaxDate(System.currentTimeMillis());
        //初始化时间
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
        Date date = null;

        int yearNow = calendar.get(Calendar.YEAR) - 18;
        int monthNow = calendar.get(Calendar.MONTH) + 1;
        int dayNow = calendar.get(Calendar.DAY_OF_MONTH);
        String years = String.valueOf(yearNow);
        String days = "";
        String months = "";

        if (dayNow < 10) {
            days = "0" + dayNow;
        } else {
            days = String.valueOf(dayNow);
        }
        if (monthNow < 10) {
            months = "0" + (monthNow - 1);
        } else {
            months = String.valueOf(monthNow);
        }

        try {
            String bitrhDay = Objects.requireNonNull(AppSettingManager.INSTANCE.readObject("user_info")).getBirthday();
            date = sdf.parse(bitrhDay);
            if (type == 0) {
                calendar.setTime(date);
                datePicker.init(Integer.parseInt(bitrhDay.split("-")[2]), Integer.parseInt(bitrhDay.split("-")[0]) - 1,
                        Integer.parseInt(bitrhDay.split("-")[1]), null);
            } else {
                calendar.setTime(sdf.parse(months + "-" + days + "-" + years));
                datePicker.init(Integer.parseInt(years), Integer.parseInt(months),
                        Integer.parseInt(days), null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return datePicker;
    }
}
